import java.util.ArrayDeque;
import java.util.Deque;

public class Sort {
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    public static void quick2(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int pivot = partition(array, start, end);
        quick(array, 0, pivot - 1);
        quick(array, pivot, end);
    }

    //优化快速排列的时间复杂度
    public static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        if (end - start + 1 <= 7) {
            insetSortRange(array, start, end);
            return;
        }
        int midIndex = getMiddleNum(array, start, end);
        swap(array, start, midIndex);
        int pivot = partition(array, start, end);
        quick(array, 0, pivot - 1);
        quick(array, pivot, end);
    }

    private static int getMiddleNum(int[] array, int left, int right) {
        int mid=(left+right)/2;
        if(array[left]<array[right]){
            if(array[mid]<array[left]){
                return left;
            }else if(array[mid]>array[right]){
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[mid]>array[left]){
                return left;
            }else if(array[mid]<array[right]){
                return right;
            }else {
                return mid;
            }
        }
    }


    private static void insetSortRange(int[] array, int start, int end) {
        for (int i = start + 1; i <= end; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= start; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    array[j + 1] = tmp;
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    private static int partition2(int[] array, int left, int right){
        int prev=left;
        int cur=left+1;
        while(cur<=right){
            if(array[cur]<array[left]&&array[++prev]!=array[cur]){
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }

    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    private static int partitionHoare(int[] array, int left, int right){
        int tmp=array[left];
        int tmpleft=left;
        while (left<right){
            while (left<right && array[right]>=tmp){
                right--;
            }
            while (left<right && array[left]<=tmp){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,tmpleft);
        return left;
    }
    public static void mergeSort(int []array){
        mergeSortTmp(array,0,array.length-1);
    }

    private static void mergeSortTmp(int[] array, int left, int right) {
        if(left>=right){
            return;
        }
        int mid=(left+right)/2;
        mergeSortTmp(array,left,mid);
        mergeSortTmp(array,mid+1,right);
        merge(array,left,mid,right);
    }

    private static void merge(int[] array, int left, int mid, int right) {
        int[] tmp = new int[right - left + 1];
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        while (s1 <= mid && s2 <= right) {
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            } else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            tmp[k++]=array[s1++];
        }
        while (s2<= right) {
            tmp[k++]=array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left]=tmp[k];
        }
    }
    public static void mergeSortNor(int[]array){
        int gap=1;
        while(gap<array.length){
            for (int i = 0; i < array.length; i=i+gap*2) {
                int left=i;
                int mid=left+gap-1;
                if(mid>=array.length){
                    mid=array.length-1;
                }
                int right=mid+gap;
                if(right>=array.length){
                    right=array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap*=2;
        }
    }
    public static void countSort(int[]array){
        int maxval=array[0];
        int minval=array[0];
        for (int i = 1; i <array.length ; i++) {
            if(array[i]<minval){
                minval=array[i];
            }
            if(array[i]>maxval){
                maxval=array[i];
            }
        }
        int len=maxval-minval+1;
        int []count=new int [len];
        for (int i = 0; i < array.length; i++) {
            int index=array[i];
            count[index-minval]++;
        }
        int index=0;
        for (int i = 0; i < count.length; i++) {
            while(count[i]!=0){
                array[index]=i+minval;
                index++;
                count[i]--;
            }
        }
    }
    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
}

